package com.hawthorn.admin.controller.sysdict;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hawthorn.admin.service.sysdict.SysDictService;
import com.hawthorn.platform.annotation.MarkLog;
import com.hawthorn.platform.exception.BizCode;
import com.hawthorn.platform.utils.common.MyUtil;
import com.hawthorn.platform.utils.iassert.AssertMyUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>todo 字典管理控制层</p>
 * @author: andy.ten@tom.com | xukaixing@hotmail.com
 * @created: 2022/7/22 16:02
 * @version v1.0.1
 * @copyright: Copyright (c)  andyten
 */
@RestController
@RequestMapping("/sysdict")
public class SysDictController
{

  @Resource
  private SysDictService sysDictService;

  /**
   * <p>todo 获取父级字典列表</p>
   * @author: guohao 2607728024@qq.com
   * @date: 2022/7/1 11:43
   * @version: 1.0.1
   */
  @ApiOperation(value = "字典管理->获取父级字典列表", notes = "获取父级字典列表")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "params", value = "params", dataTypeClass = String.class)
  })
  @PostMapping("/findParentDictList")
  public IPage<Map<String, Object>> findDictList(@RequestBody String params)
  {
    return sysDictService.findDictList(params);
  }

  /**
   * <p>todo 根据父ID获取子级列表</p>
   * @author: guohao 2607728024@qq.com
   * @date: 2022/7/1 14:36
   * @version: 1.0.1
   */
  @ApiOperation(value = "字典管理->根据父ID获取子级列表", notes = "根据父ID获取子级列表")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "String", value = "String", dataTypeClass = String.class)
  })
  @PostMapping("/findChildListByPid")
  public IPage<Map<String, Object>> findChildListByPid(@RequestBody String params)
  {
    JSONObject jsonParams = JSON.parseObject(params);
    AssertMyUtil.notNull(jsonParams.get("parent"), BizCode.VARIABLE_NOT_EMPTY, "parent");
    return sysDictService.findChildListByParent(params);
  }

  /**
   * <p>todo 字典管理父级字典修改</p>
   * @author: guohao 2607728024@qq.com
   * @date: 2022/7/4 9:33
   * @version: 1.0.1
   */
  @ApiOperation(value = "字典管理->父级字典修改", notes = "字典管理父级字典修改")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "parent", value = "parent", dataTypeClass = String.class),
      @ApiImplicitParam(name = "param", value = "param", dataTypeClass = String.class)
  })
  @MarkLog(value = "字典管理->父级字典修改-[{0}]", param = "account")
  @PutMapping(value = "/updateDictParent/{parent}")
  public Map<String, Object> updateDictParent(@PathVariable("parent") Long parent, @RequestBody Map<String, Object> param)
  {
    AssertMyUtil.notNull(parent, BizCode.VARIABLE_NOT_EMPTY, "parent");
    return sysDictService.updateDictParent(parent, param);
  }

  /**
   * <p>todo 字典管理父级字典新增</p>
   * @author: guohao 2607728024@qq.com
   * @date: 2022/7/4 9:32
   * @version: 1.0.1
   */
  @ApiOperation(value = "字典管理->父级字典新增", notes = "字典管理父级字典新增")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "param", value = "param", dataTypeClass = String.class)
  })
  @MarkLog(value = "字典管理->新增父级字典项-[{0}]", param = "parent")
  @PostMapping("/addDictParent")
  public Map<String, Object> addDictParent(@RequestBody Map<String, Object> param)
  {
    return sysDictService.saveDictParent(param);
  }

  /**
   * <p>todo 删除父级字典（包括所属子字典项）</p>
   * @author: guohao 2607728024@qq.com
   * @date: 2022/7/4 9:33
   * @version: 1.0.1
   */
  @ApiOperation(value = "字典管理-> 删除父级字典（包括所属子字典项）", notes = "删除父级字典")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "parent", value = "parent", dataTypeClass = String.class),
      @ApiImplicitParam(name = "param", value = "param", dataTypeClass = String.class)
  })
  @MarkLog(value = "字典管理->移除父级字典项-[{0}]", param = "parent")
  @PutMapping(value = "/removeDictParent/{parent}")
  public Map<String, Object> removeDictParent(@PathVariable("parent") Long parent, @RequestBody Map<String, Object> param)
  {
    AssertMyUtil.notNull(parent, BizCode.VARIABLE_NOT_EMPTY, "parent");
    if (!sysDictService.removeDictParent(parent))
    {
      AssertMyUtil.errMsg("删除失败，请重试！");
    }
    return param;
  }

  /**
   * <p>todo 字典管理子级字典修改</p>
   * @author: guohao 2607728024@qq.com
   * @date: 2022/7/4 9:33
   * @version: 1.0.1
   */
  @ApiOperation(value = "字典管理->父级字典修改", notes = "字典管理父级字典修改")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "parent", value = "parent", dataTypeClass = String.class),
      @ApiImplicitParam(name = "param", value = "param", dataTypeClass = String.class)
  })
  @MarkLog(value = "字典管理->修改子级字典项-[{0}]", param = "dicCode")
  @PutMapping(value = "/updateDictChild/{id}")
  public Map<String, Object> updateDictChild(@PathVariable("id") Long id, @RequestBody Map<String, Object> param)
  {
    AssertMyUtil.notNull(id, BizCode.VARIABLE_NOT_EMPTY, "id");
    return sysDictService.updateDictChild(id, param);
  }

  /**
   * <p>todo 字典管理子级字典新增</p>
   * @author: guohao 2607728024@qq.com
   * @date: 2022/7/4 9:32
   * @version: 1.0.1
   */
  @ApiOperation(value = "字典管理->子级字典新增", notes = "字典管理子级字典新增")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "param", value = "param", dataTypeClass = String.class)
  })
  @MarkLog(value = "字典管理->新增子级字典项-[{0}]", param = "dicCode")
  @PostMapping("/addDictChild")
  public Map<String, Object> addDictChild(@RequestBody Map<String, Object> param)
  {
    return sysDictService.saveDictChild(param);
  }

  /**
   * <p>todo 删除子级字典</p>
   * @author: andy.ten@tom.com ｜ xukaixing@hotmail.com
   * @date: 2022/7/22 15:35
   * @version: 1.0.1
   */
  @ApiOperation(value = "字典管理-> 删除子级字典", notes = "删除子级字典")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "id", dataTypeClass = Long.class),
      @ApiImplicitParam(name = "param", value = "param", dataTypeClass = String.class)
  })
  @MarkLog(value = "字典管理->新增子级字典项-[{0}]", param = "dicCode")
  @PutMapping(value = "/removeChildDict/{id}")
  public void removeChildDict(@PathVariable("id") Long id, @RequestBody Map<String, Object> param)
  {
    AssertMyUtil.notNull(id, BizCode.VARIABLE_NOT_EMPTY, "id");
    String dicCode = MyUtil.getValue(param.get("dicCode"));
    String dicType = MyUtil.getValue(param.get("dicType"));
    AssertMyUtil.notNull(dicCode, BizCode.VARIABLE_NOT_EMPTY, "dicCode");
    AssertMyUtil.notNull(dicType, BizCode.VARIABLE_NOT_EMPTY, "dicType");
    sysDictService.removeChildDict(id, dicType, dicCode);
  }
}
